<?php

//generic validate function
//that calls the page specific validate function 
//its name is constructed by using the base name of the matching php page
//i.e. for every xxx.php there should be an xxx.js


function includeValidateJS() {
	$endPos = stripos( $_SERVER['PHP_SELF'], '.php');	
	$startPos = strripos( $_SERVER['PHP_SELF'], '/');
	$jsFileName = substr($_SERVER['PHP_SELF'], $startPos + 1, $endPos - $startPos - 1);
	
	echo '<script type="text/javascript" src="javascript/' .
	$jsFileName . '.js' . '"> ';

	echo '</script>';
	
}  	

//outputs html for a button
function makeButton($buttonLabel, $submitTo) {
	$endPos = stripos( $_SERVER['PHP_SELF'], '.php');	
	$startPos = strripos( $_SERVER['PHP_SELF'], '/');
	$pageName = substr($_SERVER['PHP_SELF'], $startPos + 1, $endPos - $startPos - 1);
	$functionName = strtoupper($pageName[0]) .  substr($pageName, 1);
	echo '<div class="inactive_button"';
	echo 'onclick="javascript:document.mainform.action=';
	echo "'";
	echo $submitTo;
	echo "';";
	echo "document.mainform.buttonPressed.value='" . $buttonLabel  . "';";
	echo "if (typeof validate" . $functionName . " == 'function')  ";
		echo 'if (!(validate' . $functionName . '())) ';
			echo 'showErrorMessageDiv();';
		echo 'else document.mainform.submit(); ';
	//no validate function was defined for the page (page with no inputs)
	//call submit without validation
	echo '  else document.mainform.submit();" ';
	 

	echo ' onmouseover="this.className=';
	echo "'active_button'";
	echo '"	onMouseOut=';
	echo  '"this.className=';
	echo  "'inactive_button'";
	echo  '">';
    echo $buttonLabel;
	echo '</div>';	
}

//outputs html for a button
function makeButton1($buttonLabel, $jsFunction) {
	echo '<div class="inactive_button"';
	echo 'onclick="javascript:';
	echo $jsFunction;
	echo ';"';
	
	echo ' onmouseover="this.className=';
	echo "'active_button'";
	
	echo '"	onMouseOut=';
	echo  '"this.className=';
	echo  "'inactive_button'";
	echo  '">';
    echo $buttonLabel;
	echo '</div>';	
}


function toTitleCase($string) {
	$string = strtoupper($string[0]) .  strtolower(substr($string, 1));
	return $string;
}

require_once('database.php');

function getAvailablePostalServices($basketWeight) {
	 
		$options = getAllShippingOptions();	
	 
	$availableServices = null; 
	$maxWeight = 0;
	$maxWeightPrice = 0;
	$maxWeightServiceName = "";
	foreach ($options as $deliveryService) {
		$prices = $deliveryService['price'];
		
		foreach($prices as $weight => $price) {
			if ($weight > $maxWeight) {
				$maxWeight = $weight;
				$maxWeightPrice = $price;
				$maxWeightServiceName = $deliveryService['ds_service_name'];				
			}
			if ($basketWeight <= $weight) {
				$availableServices[$deliveryService['ds_service_name']] = $price; 
				break;
			}
		}
	}
	
	
	//if basketWeight exceeds maximum weight use the service that 
	//that allows the most weight (will have to send multiple parcels) 
	if ($availableServices == null) {
		$availableServices[$maxWeightServiceName] = $maxWeightPrice;
	}
	return $availableServices;
}
	
	
function convertBlankToNull(&$array) {
  	foreach ($array as $k => $v) {       
		if ($array[$k] == "") 
  			$array[$k] = null;   
	} 
}

function log1($s) {	

	$myFile = "logFile.txt";
	$fh = fopen($myFile, 'a') or die("can't open file");
	$theTime = strftime("%Y-%m-%d  %H:%M:%S", strtotime("now"));	   
	fwrite($fh, $theTime . ">>" . $s . "\n");
	
	fclose($fh);
	
}

function getSizeInfo($orderItemData) {
	
  		$shoulder = $orderItemData['s_shoulder'];
  		$chest = $orderItemData['s_chest'];
  		$bust = $orderItemData['s_bust'];
  		$hips = $orderItemData['s_hips'];
  		$waist = $orderItemData['s_waist'];
  		$back = $orderItemData['s_back_waist'];
  		$leg = $orderItemData['s_leg'];
  		$height = $orderItemData['s_height'];
  
  		$sizeInfo = "";
  		if ($shoulder != null && $shoulder != "") {
  			$sizeInfo .= " Shoulder: " . $shoulder . '" ';
  		}
  		if ($chest != null && $chest != "") {
  			$sizeInfo .= " Chest: " . $chest . '"';
  		}
  		if ($bust != null && $bust != "") {
  			$sizeInfo .= " Bust: " . $bust . '"';
  		}
  		if ($hips != null && $hips != "") {
  			$sizeInfo .= " Hips: " . $hips . '"';
  		}
  		if ($waist != null && $waist != "") {
  			$sizeInfo .= " Waist: " . $waist . '"';
  		}
  		if ($back != null && $back != "") {
  			$sizeInfo .= " Back: " . $back . '"';
  		}
  		if ($leg != null && $leg != "") {
  			$sizeInfo .= " Legs: " . $leg . '"';
  		}
  		if ($height != null && $height != "") {
  			$sizeInfo .= " Height: " . $height . '"';
  		}
		return $sizeInfo;
}		
?>